﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.TestSite.Objects;
using Acme.Northwind.EFDAL;
using Acme.Northwind.EFDAL.Entity;
using nHydrate.EFCore.DataAccess;

namespace Acme.TestSite
{
	public partial class TerritoryList : BasePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			grdItem.RowDataBound += new GridViewRowEventHandler(grdItem_RowDataBound);
			this.PagingControl1.ObjectSingular = "Territory";
			this.PagingControl1.ObjectPlural = "Territories";
			this.PagingControl1.RecordsPerPageChanged += new EventHandler(PagingControl1_RecordsPerPageChanged);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!this.IsPostBack)
			{
				this.Populate();
			}
		}

		#endregion

		#region Methods

		private void Populate()
		{
			using (var context = new NorthwindEntities())
			{
				List<Territory> territoryCollection = null;
				var paging = new Paging(this.PagingControl1.PageIndex, this.PagingControl1.RecordsPerPage);

				if (this.Request["regionid"] != null)
				{
					int regionId;
					int.TryParse(this.Request["regionid"], out regionId);
					territoryCollection = context.Territory.GetPagedResults(x => x.RegionId == regionId, x => x.TerritoryId, paging).ToList();
					var region = context.Region.FirstOrDefault(x => x.RegionId == regionId);
					lblHeader.Text = "This is a list of all territories for the <a href='/RegionItem.aspx?id=" + region.RegionId + "'>" + region.Name + "</a> region.";
				}
				else
				{
					territoryCollection = context.Territory.GetPagedResults(x => x.TerritoryId, paging).ToList();
					lblHeader.Text = "This is a list of all territories.";
				}

				this.PagingControl1.ItemCount = paging.RecordCount;
				grdItem.DataSource = territoryCollection;
				grdItem.DataBind();
				SessionHelper.LastTerritoryListSearch = this.Request.Url.AbsoluteUri;
			}
		}

		#endregion

		#region Event Handlers

		private void grdItem_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				using (var context = new NorthwindEntities())
				{
					var territory = e.Row.DataItem as Territory;
					var linkEdit = (HyperLink)e.Row.FindControl("linkEdit");
					var lblRegion = (HyperLink)e.Row.FindControl("lblRegion");

					linkEdit.NavigateUrl = "/TerritoryItem.aspx?id=" + territory.TerritoryId;
					lblRegion.Text = territory.Region.Name;
					lblRegion.NavigateUrl = "/RegionItem.aspx?id=" + territory.RegionId;
				}
			}
		}

		private void PagingControl1_RecordsPerPageChanged(object sender, EventArgs e)
		{
			var query = new URL(this.Request.Url.AbsoluteUri);
			query.Parameters.SetValue("rpp", PagingControl1.RecordsPerPage.ToString());
			query.Parameters.SetValue("po", "1");
			this.Response.Redirect(query.ToString());
		}

		#endregion

	}
}